﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Const;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;

namespace ServiceCore.Service
{
    /// <summary>
    /// 数组服务
    /// </summary>
    public partial class SiteService : SqlsugarServices<SiteInfo>
    {
        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<SiteInfo, List<SiteInfo>> Site_List(BaseParam<SiteInfo, List<SiteInfo>> arg)
        {
            arg.Data = this.Context.Queryable<SiteInfo>()
                .WhereIF(arg.Param != null && string.IsNullOrEmpty(arg.Param.ServicesType) == false, x => x.ServicesType == arg.Param.ServicesType)
                .WhereIF(arg.Param != null && arg.Param.CreateUserId > 0, x => x.CreateUserId == arg.Param.CreateUserId)
                .OrderBy(x => x.CreateTime)
                .ToList();
            arg.Param = null;
            if (arg.Data.Any() == false)
                return arg;

            var lstSiteId = arg.Data.Select(x => x.Id).ToList();
            var lstRecord = this.Context.Queryable<RecordInfo>().Where(x => lstSiteId.Contains(x.SiteId)).ToList(); 

            arg.Data.ForEach(x => { x.ServicesTypeName = GlobalVar.GetEnumVal(x.ServicesType); x.Total = lstRecord.Where(n => n.SiteId == x.Id).Sum(n => n.DataValue); });

            return arg;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<SiteInfo, SiteInfo> Site_Exists(BaseParam<SiteInfo, SiteInfo> arg, bool? isAdd = null)
        {
            arg.Data = this.Context.Queryable<SiteInfo>()
                .WhereIF(arg.Id > 0, x => x.Id == arg.Id)
                .WhereIF(arg.Param != null && string.IsNullOrEmpty(arg.Param.Name) == false, x => x.Name == arg.Param.Name)
                .WhereIF(isAdd.HasValue && isAdd == false, x => x.Id != arg.Param.Id)
                .First();

            return arg;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<SiteInfo, long> Site_Add(BaseParam<SiteInfo, long> arg)
        {
            arg = Site_Add_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = InsertReturnBigIdentity(arg.Param);
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<SiteInfo, long> Site_Edit(BaseParam<SiteInfo, long> arg)
        {
            arg = Site_Edit_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<SiteInfo>()
                .SetColumns(x => new SiteInfo() { Name = arg.Param.Name, ServicesType = arg.Param.ServicesType })
                .Where(x => x.Id == arg.Param.Id)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<SiteInfo, long> Site_Delete(BaseParam<SiteInfo, long> arg)
        {
            arg = Site_Del_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Deleteable<SiteInfo>().Where(x => x.Id == arg.Id).ExecuteCommand();
            arg.Param = null;

            return arg;
        }
    }
}
